package br.com.sisgerenciamento.home;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.com.sisgerenciamento.exceptions.ApplicationException;
import br.com.sisgerenciamento.facade.pedido.PedidoFacade;
import br.com.sisgerenciamento.util.Mensagem;
import br.com.sisgerenciamento.utilidades.MySQLBackup;
import br.com.sisgerenciamento.utilidades.TreatDate;

public class HomeAction extends DispatchAction{

	public ActionForward abrirHome( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){

		HomeForm homeForm = (HomeForm) form;

		Date dataFim = TreatDate.addDays(new Date(), 10); 

		try {
			homeForm.setListaPedido(PedidoFacade.getInstance().listarPedidosPorPerido(new Date(), dataFim));
		} catch (ApplicationException e) {
			e.printStackTrace();
		}

		return mapping.findForward( "home" );
	}

	public ActionForward backup( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		HomeForm homeForm = (HomeForm) form;
		try{
			MySQLBackup backup =  new MySQLBackup();
			homeForm.addMensagem(request, Mensagem.SUCESSO, "Backup efetuado com sucesso! Caminho: C:\\Backup Sistema\\"+backup.getNomeArquivo() );
		}catch (Exception e) {
			homeForm.addMensagem(request, Mensagem.ERRO, "Erro ao efetuar backup!");
		}
		return mapping.findForward( "home" );
	}
}
